//
//  SociaReleaseTextHeaderView.m
//  ShowYork
//
//  Created by hxh on 2020/9/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "SocialReleaseTextHeaderView.h"
#import "PaddingLabel.h"
@interface SocialReleaseTextHeaderView ()<UITextViewDelegate>
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UILabel *maxCount;
@property (nonatomic,strong)UILabel *placeLb;
@property (nonatomic,strong)UIView *coverView;
@property (nonatomic,strong)UILabel *tagLb;
@property (nonatomic,strong)UIButton *tagCoverButton;
@property (nonatomic,strong)UIImageView *tagImgView;
@property (nonatomic,strong)UIImageView *deleteTagImgView;
@property (nonatomic,strong)UIButton *deleteButton;
@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation SocialReleaseTextHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
        
    self.containView = [[UIView alloc]init];
    [self addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self).offset(-30);
        make.height.mas_equalTo(IPHONE_X?200:150).priorityHigh();
    }];
    
    self.textView = [[UITextView alloc]init];
    self.textView.font = font(14);
    self.textView.textColor = C102;
    self.textView.delegate = self;
    self.textView.showsVerticalScrollIndicator = NO;
    self.textView.showsHorizontalScrollIndicator = NO;
    [self.containView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.containView).offset(10);
        make.bottom.right.equalTo(self.containView).offset(-10);
    }];
    
    self.placeLb = [[UILabel alloc]init];
    self.placeLb.text = @"分享您的高考心得…";
    self.placeLb.textColor = RGBA(204, 204, 204, 1);
    self.placeLb.font = font(14);
    [self.containView addSubview:self.placeLb];
    [self.placeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView).offset(18);
        make.left.equalTo(self.containView).offset(16);
    }];
    
    self.maxCount = [[UILabel alloc]init];
    self.maxCount.text = @"0/500";
    self.maxCount.textColor = RGBA(204, 204, 204, 1);
    self.maxCount.font = font(14);
    [self.containView addSubview:self.maxCount];
    [self.maxCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView.mas_bottom);
        make.right.equalTo(self.containView).offset(-16);
    }];

    self.backgroundColor = [UIColor whiteColor];
    self.textView.backgroundColor = [UIColor whiteColor];
    
    self.coverView = [[UIView alloc]init];
    self.coverView.backgroundColor = RGBA(202, 202, 202, 1);
    self.coverView.layer.cornerRadius = 13;
    self.coverView.layer.masksToBounds = YES;
    [self addSubview:self.coverView];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.maxCount).offset(-6);
        make.left.equalTo(self.containView).offset(16);
        make.height.mas_equalTo(26);
    }];
    
    self.tagCoverButton = [[UIButton alloc]init];
    [self.tagCoverButton addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.coverView addSubview:self.tagCoverButton];
    [self.tagCoverButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.coverView);
    }];
    
    self.tagImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_page_#_tag"]];
    [self.coverView addSubview:self.tagImgView];
    [self.tagImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.coverView);
        make.left.equalTo(self.coverView).offset(12);
    }];
    
    self.tagLb = [[UILabel alloc]init];
    self.tagLb.text = @"#话题";
    self.tagLb.textColor = UIColor.whiteColor;
    self.tagLb.font = font(14);
    [self.coverView addSubview:self.tagLb];
    [self.tagLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tagImgView.mas_right).offset(4);
        make.right.equalTo(self.coverView).offset(-12);
        make.centerY.equalTo(self.coverView);
    }];


    
    self.deleteTagImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"social_delete_tag"]];
    [self.coverView addSubview:self.deleteTagImgView];
    [self.deleteTagImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.coverView);
        make.left.equalTo(self.tagLb.mas_right).offset(12);
    }];
    
    self.deleteButton = [[UIButton alloc]init];
    [self.deleteButton addTarget:self action:@selector(deleteMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.coverView addSubview:self.deleteButton];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_lessThanOrEqualTo(self.coverView);
        make.left.equalTo(self.deleteTagImgView.mas_left).offset(-6);
    }];
    
    self.deleteButton.hidden = YES;
    self.deleteTagImgView.hidden = YES;
    
    self.tagLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverMethod)];
    [self.tagLb addGestureRecognizer:tap];
    

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstReleaseSocial"]boolValue]) {
        [self.textView becomeFirstResponder];
    }
}
- (void)beginTextView{
    [self.textView becomeFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView{
    self.placeLb.hidden = textView.text.length > 0;
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 500){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:500];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:500];
                if (toBeString.length > 500) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
                
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,500)];
                textView.text = [toBeString substringWithRange:rangeRange];
           
                if (rangeRange.length < toBeString.length) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }
        }
    }
    self.maxCount.text = [NSString stringWithFormat:@"%lu/500",MIN(500, (unsigned long)textView.text.length)];
    if (self.delegate && [self.delegate respondsToSelector:@selector(getReleaseText:)]) {
        [self.delegate getReleaseText:textView.text];
    }
}


- (void)setContent:(NSString *)content andBindTagModel:(SocialSubTopicModel *)tagModel{
    self.textView.text = content;
    self.placeLb.hidden = content.length > 0;
    self.maxCount.text = [NSString stringWithFormat:@"%lu/500",MIN(500, content.length)];
    if (tagModel != nil) {
        self.tagLb.text = tagModel.topicName;
        [self.tagLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.coverView).offset(-30);
        }];
        self.deleteButton.hidden = NO;
        self.deleteTagImgView.hidden = NO;
    }else{
        self.tagLb.text = @"话题";
        [self.tagLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.coverView).offset(-12);
        }];
        self.deleteButton.hidden = YES;
        self.deleteTagImgView.hidden = YES;
    }
}


- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showReleaseTagList)]) {
        [self.delegate showReleaseTagList];
    }
}
- (void)deleteMethod{
    self.deleteButton.hidden = YES;
    self.deleteTagImgView.hidden = YES;
    [self.tagLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.coverView).offset(-12);
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(deleteCurrentTag)]) {
        [self.delegate deleteCurrentTag];
    }
}

@end
